unit AddLinkedItemGui;

interface

uses
  Items, GlobalRecordsAndEnums,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfrmAddLinkedItem = class(TForm)
    btnOk: TButton;
    btnCancel: TButton;
    lblCaption: TLabel;
    lblAmount: TLabel;
    edtAmount: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    edtDamage: TEdit;
    chkWaste: TCheckBox;
    lblWaste: TLabel;
    procedure btnOkClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnCancelClick(Sender: TObject);
  private
  public
//    result: SItemAmount;
    result: TItemAmount;
    allowedtoclose: Boolean;

    constructor Create(AOwner: TComponent; item, linkeditem: TNewItem; activity: EActivity); reintroduce;
  end;

var
  frmAddLinkedItem: TfrmAddLinkedItem;

implementation

{$R *.dfm}

//uses

constructor TfrmAddLinkedItem.Create(AOwner: TComponent; item, linkeditem: TNewItem; activity: EActivity);
begin
  inherited Create(AOwner);

  case activity of
    ACT_Copying: lblCaption.Caption := 'You are adding ' + linkeditem.Name + ' as an item used in the copying of a ' + item.Name + ' BPO.';
    ACT_Invention: lblCaption.Caption := 'You are adding ' + linkeditem.Name + ' as an item used in the invention of a ' + item.Name + '.';
    ACT_None: lblCaption.Caption := 'You are adding ' + linkeditem.Name + ' as a result when inventing a ' + item.Name + '.';
    ACT_Manufacture: lblCaption.Caption := 'You are adding ' + linkeditem.Name + ' as an item used in the manufacturing of ' + item.Name + '.';
  end;
  lblCaption.Width := 265;
  result := TItemAmount.Create;
  result.ID := linkeditem.ID;
  result.Item := linkeditem;
  result.Activity := activity;

  allowedtoclose := false;
end;

procedure TfrmAddLinkedItem.btnOkClick(Sender: TObject);
var
  s: String;
  i: Integer;
  d: Double;
begin
  s := StringReplace(edtDamage.Text, '.', global_ds, [rfReplaceAll]);
  if ((TryStrToInt(edtAmount.Text, i)) and (TryStrToFloat(s, d)) and (d >= 0) and (d <= 1)) then begin
    result.Amount := i;
    result.Damage := d;
    result.ApplyWaste := chkWaste.Checked;
    allowedtoclose := true;
  end
  else begin
    edtAmount.Color := clWindow;
    edtDamage.Color := clWindow;
    if (not (TryStrToInt(edtAmount.Text, i))) then edtAmount.Color := clRed;
    if (not ((TryStrToFloat(s, d))) or (d >= 0) or (d <= 1)) then edtDamage.Color := clRed;
  end;
end;

procedure TfrmAddLinkedItem.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  CanClose := allowedtoclose;
end;

procedure TfrmAddLinkedItem.btnCancelClick(Sender: TObject);
begin
  result.Free;
  allowedtoclose := true;
end;

end.
